package com.libbase.bugly;

import android.content.Context;
import android.text.TextUtils;

import com.libbase.BuildConfig;
import com.meituan.android.walle.WalleChannelReader;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.crashreport.CrashReport;
import com.uitls.utils.AppUtil;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import static com.tencent.bugly.beta.tinker.TinkerManager.getApplication;


/**
 * Created by dell on 2018/1/26.
 */

public class BuglyHelper {

    private static BuglyHelper instance;

    public static BuglyHelper getInstance() {
        if (instance == null) {
            synchronized (BuglyHelper.class) {
                if (instance == null) {
                    instance = new BuglyHelper();
                }
            }
        }
        return instance;
    }


    public void init(Context context) {
        //if (BuildConfig.ISRELEASE) { // 线上
        // 设置是否开启热更新能力，默认为true
        Beta.enableHotfix = true;
        // 设置是否自动下载补丁，默认为true
        Beta.canAutoDownloadPatch = true;
        // 设置是否自动合成补丁，默认为true
        Beta.canAutoPatch = true;
        // 设置是否提示用户重启，默认为false
        Beta.canNotifyUserRestart = false;

        Bugly.setIsDevelopmentDevice(context, true);

        // 多渠道需求塞入
        String channel = WalleChannelReader.getChannel(context);
        if (TextUtils.isEmpty(channel)) {
            channel = "libBase";
        }
        Bugly.setAppChannel(getApplication(), channel);

        CrashReport.UserStrategy userStrategy = new CrashReport.UserStrategy(context);
        userStrategy.setAppChannel(channel);
        userStrategy.setDeviceID(AppUtil.generateDeviceId(context));
        userStrategy.setAppPackageName(context.getPackageName());
        userStrategy.setAppVersion(BuildConfig.VERSION_NAME);

        // 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
        userStrategy.setUploadProcess(processName == null || processName.equals(context.getPackageName()));

        //CrashReport.initCrashReport(context, "ced0cf47d7", true, userStrategy);
        Bugly.init(context, "ced0cf47d7", true, userStrategy);
//        } else { // 测试
//            Bugly.setIsDevelopmentDevice(context, true);
//
//            CrashReport.initCrashReport(context, "ced0cf47d7", true);
//            Bugly.init(context, "ced0cf47d7", true);
//        }
    }

    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }
}
